package sim.ring.method;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Vector;

import org.apache.log4j.Logger;

import sim.ring.technical.data.DatabaseConnection;

public class MethodQueryBuilder {
	private static Logger log = Logger.getLogger("sim.ring.method");
	
	public static void saveMethod(Method method) {
		DatabaseConnection db = new DatabaseConnection();
		try {
			db.connectToDB("RingingSimDB");
		} catch (SQLException e1) {
			Iterator<Throwable> it = e1.iterator();
			while (it.hasNext()) {
				log.error(it.next().getMessage());
			}
		}
		
		String sql = "";
		int id=method.getId();
		Vector<Object> params = new Vector<Object>();
		
		if (id>0) {
			sql = "UPDATE methods SET "
					+"name=?,"
					+"classification=?,"
					+"stage=?,"
					+"short_notation=?,"
					+"long_notation=?,"
					+"ring_count=? "
					+ "WHERE id=?";
			
			params.add(method.getName());
			params.add(method.getClassification());
			params.add(new Integer(method.getStage()));
			params.add(method.getShortNotation());
			params.add(method.getLongNotation());
			params.add(new Integer(method.getRingCount()));
			params.add(new Integer(method.getId()));
		} else {
			sql = "INSERT INTO methods ("
					+"name,"
					+"classification,"
					+"stage,"
					+"short_notation,"
					+"long_notation,"
					+"ring_count)"
					+"VALUES ("
					+"?,"
					+"?,"
					+"?,"
					+"?,"
					+"?,"
					+"?"
					+")";
			
			params.add(method.getName());
			params.add(method.getClassification());
			params.add(new Integer(method.getStage()));
			params.add(method.getShortNotation());
			params.add(method.getLongNotation());
			params.add(new Integer(method.getRingCount()));
		}
		
		try {
			int key = db.issueNonQueryStatement(sql, params);
			if (id==-1) {
				id=key;
				method.setId(id);
			}	
		} catch (SQLException e) {
			Iterator<Throwable> it = e.iterator();
			while (it.hasNext()) {
				log.error(it.next().getMessage());
			}
		}
		db.closeDB();
	}
	
	public static Method loadMethod(int id) {
		String name="";
		String classification="";
		int stage=0;
		String shortNotation="";
		String longNotation="";
		int ringCount=0;
		
		DatabaseConnection db = new DatabaseConnection();
		try {
			db.connectToDB("RingingSimDB");
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		String sql = "SELECT * FROM methods " 
					+ "WHERE id=?";
		
		Vector<Object> params = new Vector<Object>();
		params.add(new Integer(id));
		
		ResultSet rs=null;
		try {
			rs=db.issueQueryStatement(sql, params);
			rs.next();
			id=rs.getInt(1);
			name=rs.getString(2);
			classification=rs.getString(3);
			stage=rs.getInt(4);
			shortNotation=rs.getString(5);
			longNotation=rs.getString(6);
			ringCount=rs.getInt(7);
		} catch (SQLException e) {
			//TODO
			log.error(e.getMessage());
		} finally {
			//TODO: close everything
		}
		db.closeDB();
		return new Method(id, name, classification, stage, shortNotation, longNotation, ringCount);
	}
	
}
